package com.springbootbase.springbootproject.service.impl;

import com.springbootbase.springbootproject.mapper.StudentMapper;
import com.springbootbase.springbootproject.pojo.ListResponse;
import com.springbootbase.springbootproject.pojo.Student;
import com.springbootbase.springbootproject.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class StudentImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);
    }

    @Override
    public Student findById(Long id) {
        Student student = studentMapper.findById(id);
        return student;
    }

    @Override
    public ListResponse getList(Long id, Integer pageSize, Integer pageNum) {
        int offset = (pageNum-1)*pageSize;
        ArrayList<Student> studentArrayList = studentMapper.getList(id,offset,pageSize);
        for (Student i : studentArrayList){
            String name = studentMapper.getNameById(i.getId());
            i.setName(name);
        }
        int listSize = studentMapper.countAllStudentInfo(id);
        ListResponse listResponse = new ListResponse(studentArrayList,listSize);
        return  listResponse;
    }

}
